#include "CAI.h"
#include "CMap.h"
#include "Game Objects\CNode.h"
#include "Game Objects\CPlayer.h"
#include "CMap.h"
#include "Game Objects\CTeleporter.h"
#include "Wrappers\CSGD_Direct3D.h"



CAi::CAi()
{
	capturerate = 25.0f;
	totaltime = 0.0f;
	chosen = NULL;
	playerat = NULL;
	map = NULL;
	target = NULL;
	Home = NULL;
	player = CPlayer::GetInstance();
}

void CAi::MakeDec()
{
	bool temp = false;
	NodesAvailiable.clear();
	for(unsigned int i = 0; i < NodesOwnd.size(); i++)
	{
		for(unsigned int j = 0; j < NodesOwnd[i]->getTeles()->size(); j++)
		{
			if(NodesOwnd[i]->getTeles()->operator[](j)->getLink()->getNode()->GetOwner() != OWNER_AI)
			{
				NodesAvailiable.push_back(NodesOwnd[i]->getTeles()->operator[](j)->getLink()->getNode());

			}
		}
	}



	for(unsigned int i = 0; i < NodesAvailiable.size(); i++)
	{
		if(NodesAvailiable[i] == target)
		{
			goforthethroat();
			return;
		}
	}

	if(map->getcurNode() == Home)
	{
		youlittleprick();
		return;
	}

	if(chosen == NULL)
	{
		igotyou();
	}

	if(chosen == NULL)
	{
	RandomChoice();
	}





}

void CAi::RandomChoice()
{

	chosen = NodesAvailiable[rand() % (int)NodesAvailiable.size()];
}
void CAi::goforthethroat()
{
	chosen = target;
}

void CAi::igotyou()
{
	int connected = 0;
	CNode* temp;
	for(unsigned int i = 0; i < playerat->getTeles()->size(); i++)
	{
		if( playerat->getTeles()->operator[](i)->getLink()->getNode()->GetOwner() == OWNER_PLAYER)
		{
			connected++;
			temp = playerat->getTeles()->operator[](i)->getLink()->getNode();
		}

	}
	if(connected == 1)
	{
		for(unsigned int i = 0; i < NodesAvailiable.size(); i++)
		{
			if(temp == NodesAvailiable[i])
			{
				chosen = temp;
				
			}
		}
	}
}

void CAi::youlittleprick()
{
	for(unsigned int i = 0; i < Home->getTeles()->size();i++)
	{
		if(Home->getTeles()->operator[](i)->getNode()->GetOwner() == OWNER_PLAYER)
		{
			chosen = Home->getTeles()->operator[](i)->getNode();
			break;
		}

	}
}

void CAi::Render()
{
	/*char buffer[128];
	if(map != NULL)
	{
		
		std::vector<CNode*>* nodes = map->getNodes();
		for(unsigned int i = 0; i < nodes->size(); i++)
		{
			if(nodes->operator[](i)->GetOwner() == OWNER_AI)
			{
				sprintf_s(buffer, _countof(buffer), "I own node: %i", i);
				CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 0, (i * 10) + 100, 0, 0, 255);
			}
			if(nodes->operator[](i) == chosen)
			{
				sprintf_s(buffer, _countof(buffer), "I am capturing node: %i", i);
				CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 0, (i * 10) + 100, 0, 0, 255);
			}
		}
		
	}
	else
	{
		
		CSGD_Direct3D::GetInstance()->DrawTextA("I Win!", 0,100, 0, 0, 255);
		map = NULL;
	}*/




}

void CAi::SetMap(CMap* themap)
{
	map = themap;
}
void CAi::SetTartget(CNode* thetarget)
{
	target = thetarget;
}
void CAi::SetHome(CNode* thehome)
{
	Home = thehome;
}

void CAi::Update(float fElapsedTime)
{
	//AM i even playing?
	if(map != NULL && target != NULL && Home != NULL && target->GetOwner() != OWNER_AI)
	{
		//populate database
		NodesOwnd.clear();
		Nodesplayer.clear();
		std::vector<CNode*>* nodes = map->getNodes();
		for(unsigned int i = 0; i < nodes->size(); i++)
		{
			if(nodes->operator[](i)->GetOwner() == OWNER_AI)
			{
				NodesOwnd.push_back(nodes->operator[](i));
			}
			if(nodes->operator[](i)->GetOwner() == OWNER_PLAYER)
			{
				Nodesplayer.push_back(nodes->operator[](i));
			}
		}
		
		playerat = map->getcurNode();
		//Ai destroyed player
		if(Nodesplayer.size() == 0 || target->GetOwner() == OWNER_AI)
		{
			map = NULL;
			return;
		}
		//utter destruction
		if(NodesOwnd.size() == 0)
		{
			map = NULL;
			return;
		}
		//time to make a desicion
		if(chosen == NULL)
		{
			MakeDec();
		}
		//wait to capture node
		else
		{
			//check to see if ai lost connection to node
			std::vector<CTeleporter*>* teles = chosen->getTeles();
			for(unsigned int i = 0; i < teles->size(); i++)
			{
				if(teles->operator[](i)->getLink()->getNode()->GetOwner() == OWNER_AI)
				{
					break;
				}
				if(i == teles->size() - 1)
				{
					chosen = NULL;
					totaltime = 0.0f;
					return;
				}
			}
			//increment time till node capture
			totaltime += fElapsedTime;
			if(totaltime >= capturerate)
			{

				chosen->SetOwner(OWNER_AI);
				totaltime = 0.0f;
				chosen = NULL;

			}
		}
	}
}

void CAi::killai()
{
	map = NULL;
	chosen = NULL;
	target = NULL;
	Home = NULL;
}

